{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "dcpGQid2Jcud"
      },
      "source": [
        "# 📘 Hackathon Project Video Evaluator using PraisonAIAgents"
      ],
      "id": "dcpGQid2Jcud"
    },
    {
      "cell_type": "markdown",
      "source": [
        "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/DhivyaBharathy-web/PraisonAI/blob/main/examples/cookbooks/Hackathon_Agent_Notebook.ipynb)\n"
      ],
      "metadata": {
        "id": "9A8PW-_1NyK4"
      },
      "id": "9A8PW-_1NyK4"
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Dependencies"
      ],
      "metadata": {
        "id": "B-RlKMC3NWZL"
      },
      "id": "B-RlKMC3NWZL"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "ItoyCaVhJcug"
      },
      "outputs": [],
      "source": [
        "!pip install praisonaiagents opencv-python moviepy"
      ],
      "id": "ItoyCaVhJcug"
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Set OpenAI API Key for Authentication"
      ],
      "metadata": {
        "id": "x4dBCoN-Nf4h"
      },
      "id": "x4dBCoN-Nf4h"
    },
    {
      "cell_type": "code",
      "source": [
        "import os\n",
        "os.environ[\"OPENAI_API_KEY\"] = \"Enter your api key\"\n",
        "print(\"OpenAI API key has been set successfully!\")"
      ],
      "metadata": {
        "id": "wz9hEZf1Ncv2"
      },
      "id": "wz9hEZf1Ncv2",
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Setup: Imports and Agent Frameworks"
      ],
      "metadata": {
        "id": "L8UiiaAiNtRQ"
      },
      "id": "L8UiiaAiNtRQ"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "eU6iiDynJcuh"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "import json\n",
        "import re\n",
        "import cv2\n",
        "import numpy as np\n",
        "from typing import List\n",
        "from pydantic import BaseModel\n",
        "from praisonaiagents import Agent, Task, PraisonAIAgents"
      ],
      "id": "eU6iiDynJcuh"
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "jIBUGHAbJcui"
      },
      "source": [
        "## 🛠️ Tools and Data Models"
      ],
      "id": "jIBUGHAbJcui"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "3wGOzkZcJcuj"
      },
      "outputs": [],
      "source": [
        "class ProjectEvaluation(BaseModel):\n",
        "    innovation_score: int\n",
        "    technical_complexity: int\n",
        "    presentation_quality: int\n",
        "    user_experience: int\n",
        "    completeness: int\n",
        "    overall_score: int\n",
        "    key_strengths: List[str]\n",
        "    areas_for_improvement: List[str]\n",
        "    notable_features: List[str]\n",
        "    technical_highlights: List[str]\n",
        "    recommendations: List[str]\n",
        "    market_potential: str\n",
        "    scalability_assessment: str"
      ],
      "id": "3wGOzkZcJcuj"
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "idRWbOQNJcuj"
      },
      "source": [
        "## 🎞️ Video Frame Extraction"
      ],
      "id": "idRWbOQNJcuj"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Rlhm_jzfJcuj"
      },
      "outputs": [],
      "source": [
        "def extract_frames(video_path: str, num_frames: int = 5) -> List[str]:\n",
        "    if not os.path.exists(video_path):\n",
        "        raise FileNotFoundError(f\"Video not found: {video_path}\")\n",
        "    frames_dir = os.path.join(os.path.dirname(video_path), \"frames\")\n",
        "    os.makedirs(frames_dir, exist_ok=True)\n",
        "    cap = cv2.VideoCapture(video_path)\n",
        "    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))\n",
        "    interval = total_frames // (num_frames + 1)\n",
        "    paths = []\n",
        "    for i in range(1, num_frames + 1):\n",
        "        cap.set(cv2.CAP_PROP_POS_FRAMES, i * interval)\n",
        "        ret, frame = cap.read()\n",
        "        if not ret: break\n",
        "        path = os.path.join(frames_dir, f\"frame_{i}.jpg\")\n",
        "        cv2.imwrite(path, frame)\n",
        "        paths.append(path)\n",
        "    cap.release()\n",
        "    return paths"
      ],
      "id": "Rlhm_jzfJcuj"
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "97Bap7zKJcuk"
      },
      "source": [
        "## 🤖 Define the AI Agent"
      ],
      "id": "97Bap7zKJcuk"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "KLME0GcsJcuk"
      },
      "outputs": [],
      "source": [
        "hackathon_judge = Agent(\n",
        "    name=\"HackathonJudge\",\n",
        "    role=\"Technical Project Evaluator\",\n",
        "    goal=\"Evaluate hackathon projects through video demonstrations\",\n",
        "    backstory=\"\"\"You are an expert hackathon judge. You give fair scores and detailed feedback\n",
        "    on technical complexity, innovation, presentation, and UX.\"\"\",\n",
        "    llm=\"gpt-5-nano\",\n",
        "    self_reflect=False\n",
        ")"
      ],
      "id": "KLME0GcsJcuk"
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "HG5dmlzhJcul"
      },
      "source": [
        "## 🚀 Main Evaluation Function"
      ],
      "id": "HG5dmlzhJcul"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "6ioAIkG4Jcul"
      },
      "outputs": [],
      "source": [
        "def evaluate_project(video_path: str) -> ProjectEvaluation:\n",
        "    frame_paths = extract_frames(video_path)\n",
        "    task = Task(\n",
        "        name=\"project_evaluation\",\n",
        "        description=\"\"\"Analyze hackathon video frames and score: innovation, complexity, UX,\n",
        "        presentation, completeness. Provide strengths, improvements, highlights, market potential,\n",
        "        and recommendations.\"\"\",\n",
        "        expected_output=\"JSON feedback with scores and suggestions\",\n",
        "        agent=hackathon_judge,\n",
        "        output_pydantic=ProjectEvaluation,\n",
        "        images=frame_paths\n",
        "    )\n",
        "    agents = PraisonAIAgents(\n",
        "        agents=[hackathon_judge],\n",
        "        tasks=[task],\n",
        "        process=\"sequential\",\n",
        "        verbose=True\n",
        "    )\n",
        "\n",
        "    try:\n",
        "        response = agents.start()\n",
        "        if isinstance(response, dict) and 'task_results' in response:\n",
        "            output = response['task_results'][0]\n",
        "            if hasattr(output, 'pydantic'):\n",
        "                return output.pydantic\n",
        "            elif hasattr(output, 'raw'):\n",
        "                raw = output.raw.strip()\n",
        "                json_text = re.search(r'```(?:json)?\\s*([\\s\\S]+?)\\s*```', raw)\n",
        "                raw = json_text.group(1).strip() if json_text else raw\n",
        "                return ProjectEvaluation(**json.loads(raw))\n",
        "        elif isinstance(response, str):\n",
        "            return ProjectEvaluation(**json.loads(response))\n",
        "        return ProjectEvaluation(**response)\n",
        "    finally:\n",
        "        for path in frame_paths:\n",
        "            os.remove(path)\n",
        "        try:\n",
        "            os.rmdir(os.path.join(os.path.dirname(video_path), \"frames\"))\n",
        "        except: pass"
      ],
      "id": "6ioAIkG4Jcul"
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VAwiZiJ1Jcum"
      },
      "source": [
        "## ▶️ Run the Evaluation"
      ],
      "id": "VAwiZiJ1Jcum"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Jf9_efNPJcum"
      },
      "outputs": [],
      "source": [
        "os.environ[\"OPENAI_API_KEY\"] = \"sk-proj-...YOUR_API_KEY...\"\n",
        "\n",
        "video_file = \"presentation.mp4\"\n",
        "try:\n",
        "    result = evaluate_project(video_file)\n",
        "    print(\"\\n🔍 Hackathon Project Evaluation\")\n",
        "    print(\"=\" * 40)\n",
        "    print(f\"Overall Score: {result.overall_score}/100\")\n",
        "    print(f\"Innovation: {result.innovation_score}/100\")\n",
        "    print(f\"Technical Complexity: {result.technical_complexity}/100\")\n",
        "    print(f\"Presentation Quality: {result.presentation_quality}/100\")\n",
        "    print(f\"User Experience: {result.user_experience}/100\")\n",
        "    print(f\"Completeness: {result.completeness}/100\")\n",
        "    print(\"\\n✅ Key Strengths:\")\n",
        "    for s in result.key_strengths: print(f\"- {s}\")\n",
        "    print(\"\\n⚠️ Areas for Improvement:\")\n",
        "    for a in result.areas_for_improvement: print(f\"- {a}\")\n",
        "    print(\"\\n🛠️ Technical Highlights:\")\n",
        "    for t in result.technical_highlights: print(f\"- {t}\")\n",
        "    print(\"\\n📌 Recommendations:\")\n",
        "    for r in result.recommendations: print(f\"- {r}\")\n",
        "    print(f\"\\n📈 Market Potential: {result.market_potential}\")\n",
        "    print(f\"\\n📊 Scalability Assessment: {result.scalability_assessment}\")\n",
        "except FileNotFoundError:\n",
        "    print(\"❗ 'presentation.mp4' not found. Please add it to the current directory.\")\n",
        "except Exception as e:\n",
        "    print(f\"❌ Evaluation Error: {e}\")"
      ],
      "id": "Jf9_efNPJcum"
    },
    {
      "cell_type": "code",
      "source": [
        "output = \"\"\"\n",
        "╭─ Agent Info ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮\n",
        "│                                                                                                                              │\n",
        "│  👤 Agent: HackathonJudge                                                                                                    │\n",
        "│  Role: Technical Project Evaluator                                                                                           │\n",
        "│                                                                                                                              │\n",
        "╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
        "\n",
        "╭───────────────────────────────────────────────────── Generating... 6.6s ─────────────────────────────────────────────────────╮\n",
        "│                                                                                                                              │\n",
        "│  {                                                                                                                           │\n",
        "│    \"innovation_score\": 30,                                                                                                   │\n",
        "│    \"technical_complexity\": 20,                                                                                               │\n",
        "│    \"presentation_quality\": 25,                                                                                               │\n",
        "│    \"user_experience\": 35,                                                                                                    │\n",
        "│    \"completeness\": 20,                                                                                                       │\n",
        "│    \"overall_score\": 24,                                                                                                      │\n",
        "│    \"key_strengths\": [                                                                                                        │\n",
        "│      \"Simple concept\",                                                                                                       │\n",
        "│      \"Clear visual representation\"                                                                                           │\n",
        "│    ],                                                                                                                        │\n",
        "│    \"areas_for_improvement\": [                                                                                                │\n",
        "│      \"Enhance technical features\",                                                                                           │\n",
        "│      \"Improve presentation clarity\",                                                                                         │\n",
        "│      \"Add user interaction elements\"                                                                                         │\n",
        "│    ],                                                                                                                        │\n",
        "│    \"notable_features\": [                                                                                                     │\n",
        "│      \"Basic visual interface\"                                                                                                │\n",
        "│    ],                                                                                                                        │\n",
        "│    \"technical_highlights\": [                                                                                                 │\n",
        "│      \"Use of basic graphics\"                                                                                                 │\n",
        "│    ],                                                                                                                        │\n",
        "│    \"recommendations\": [                                                                                                      │\n",
        "│      \"Incorporate user feedback mechanisms\",                                                                                 │\n",
        "│      \"Develop a more robust backend\",                                                                                        │\n",
        "│      \"Enhance visual aesthetics\"                                                                                             │\n",
        "│    ],                                                                                                                        │\n",
        "│    \"market_potential\": \"Limited due to simplicity\",                                                                          │\n",
        "│    \"scalability_assessment\": \"Low, requires significant development\"                                                         │\n",
        "│  }                                                                                                                           │\n",
        "│                                                                                                                              │\n",
        "╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
        "\"\"\"\n",
        "print(output)\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "T-wYaKzkKGNK",
        "outputId": "ebeaf7e3-6362-4d1c-f9d1-cc3504d733a2"
      },
      "id": "T-wYaKzkKGNK",
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\n",
            "╭─ Agent Info ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮\n",
            "│                                                                                                                              │\n",
            "│  👤 Agent: HackathonJudge                                                                                                    │\n",
            "│  Role: Technical Project Evaluator                                                                                           │\n",
            "│                                                                                                                              │\n",
            "╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
            "\n",
            "╭───────────────────────────────────────────────────── Generating... 6.6s ─────────────────────────────────────────────────────╮\n",
            "│                                                                                                                              │\n",
            "│  {                                                                                                                           │\n",
            "│    \"innovation_score\": 30,                                                                                                   │\n",
            "│    \"technical_complexity\": 20,                                                                                               │\n",
            "│    \"presentation_quality\": 25,                                                                                               │\n",
            "│    \"user_experience\": 35,                                                                                                    │\n",
            "│    \"completeness\": 20,                                                                                                       │\n",
            "│    \"overall_score\": 24,                                                                                                      │\n",
            "│    \"key_strengths\": [                                                                                                        │\n",
            "│      \"Simple concept\",                                                                                                       │\n",
            "│      \"Clear visual representation\"                                                                                           │\n",
            "│    ],                                                                                                                        │\n",
            "│    \"areas_for_improvement\": [                                                                                                │\n",
            "│      \"Enhance technical features\",                                                                                           │\n",
            "│      \"Improve presentation clarity\",                                                                                         │\n",
            "│      \"Add user interaction elements\"                                                                                         │\n",
            "│    ],                                                                                                                        │\n",
            "│    \"notable_features\": [                                                                                                     │\n",
            "│      \"Basic visual interface\"                                                                                                │\n",
            "│    ],                                                                                                                        │\n",
            "│    \"technical_highlights\": [                                                                                                 │\n",
            "│      \"Use of basic graphics\"                                                                                                 │\n",
            "│    ],                                                                                                                        │\n",
            "│    \"recommendations\": [                                                                                                      │\n",
            "│      \"Incorporate user feedback mechanisms\",                                                                                 │\n",
            "│      \"Develop a more robust backend\",                                                                                        │\n",
            "│      \"Enhance visual aesthetics\"                                                                                             │\n",
            "│    ],                                                                                                                        │\n",
            "│    \"market_potential\": \"Limited due to simplicity\",                                                                          │\n",
            "│    \"scalability_assessment\": \"Low, requires significant development\"                                                         │\n",
            "│  }                                                                                                                           │\n",
            "│                                                                                                                              │\n",
            "╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
            "\n"
          ]
        }
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.11"
    },
    "colab": {
      "provenance": []
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}